package com.fire.control.system.module.common.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fire.control.system.dto.SelectCompanyDTO;
import com.fire.control.system.dto.SelectDeviceGroupDTO;
import com.fire.control.system.dto.SelectDeviceTypeDTO;
import com.fire.control.system.dto.SelectGatewayDTO;
import com.fire.control.system.dto.SelectGroupDTO;
import com.fire.control.system.dto.SelectRoleDTO;
import com.fire.control.system.dto.SelectUserDTO;
import com.fire.control.system.dto.SelectWarningDescDTO;
import com.fire.control.system.dto.UserDTO;
import com.fire.control.system.module.common.service.SelectService;
import com.fire.control.system.module.company.dao.CompanyMapper;
import com.fire.control.system.module.company.service.CompanyService;
import com.fire.control.system.module.device.dao.DeviceGroupMapper;
import com.fire.control.system.module.device.dao.DeviceMapper;
import com.fire.control.system.module.device.dao.GatewayMapper;
import com.fire.control.system.module.device.service.DeviceGroupService;
import com.fire.control.system.module.role.dao.RoleMapper;
import com.fire.control.system.module.user.dao.UserMapper;
import com.fire.control.system.module.warning.dao.WarningProtocolMapper;
import com.fire.control.system.statics.ResponseMessage;
import com.fire.control.system.support.protocol.comman.ProtocolSelector;
import com.fire.control.system.support.protocol.comman.ResponseProtocol;
import com.fire.control.system.support.protocol.common.SelectRequestProtocol;
import com.fire.control.system.support.util.XdaoDate;

/**
 * 
 * @ClassName:  SelectServiceImpl
 * @Description:  下拉选择服务具体实现
 * @author:  xiechengguang
 * @date:  2017年6月17日 上午11:15:45
 *
 */
@Service
public class SelectServiceImpl implements SelectService {
	
	@Autowired private ProtocolSelector protocolSelector;
	@Autowired private DeviceGroupMapper deviceGroupMapper;
	@Autowired private DeviceMapper deviceMapper;
	@Autowired private UserMapper userMapper;
	@Autowired private CompanyMapper companyMapper;
	@Autowired private RoleMapper roleMapper;
	@Autowired private GatewayMapper gatewayMapper;
	@Autowired private WarningProtocolMapper warningProtocolMapper;
	@Autowired private CompanyService companyService;
	@Autowired private DeviceGroupService deviceGroupService;
	
	
	/**
	 * @author: xiechengguang 
	 * @Title: select   
	 * @Description:  
	 *   通过type:区分加载数据类型：1.企业    2.设备组   3.用户：安全管理员/巡检员 （根据设备组id） 
	 *   4.设备组的所属组（下拉选） 5.下拉选报警描述类型   6.角色： 管理员 /安全管理员/巡检员
	 * @param:        
	 * @return: ResponseProtocol      
	 * @throws
	 */
	@Override
	public ResponseProtocol select(SelectRequestProtocol request, UserDTO user) {
		ResponseProtocol respProtocol = protocolSelector.choose(request);
		
		int type = request.getType();
		String id = request.getId();
		
		String companyId = user.getCompany().getId();
		
		
		if(type == 1) {
			// 设备类型
			List<SelectDeviceTypeDTO> dtList = deviceMapper.getAllDeviceType();
			// 企业
//			List<SelectCompanyDTO> cList = companyMapper.getCompanys(id==null?companyId:id);
			List<SelectCompanyDTO> cList = new ArrayList<>();
			if(id == null) {
				cList = companyService.getSelectCompanyList(companyId);
			} else {
				SelectCompanyDTO dto = companyMapper.getCompanyDto(id);
				cList.add(dto);
			}
			
			Map<String, Object> returnMap = new HashMap<String, Object>();
			returnMap.put("deviceType", dtList);
			returnMap.put("company", cList);
			
			ResponseMessage.CODE_200.messageToProtocol(respProtocol);
			respProtocol.setDate(new XdaoDate());
			respProtocol.setData(returnMap);
			return respProtocol;
			
		} if(type == 2) {  // 根据企业id获取设备组
//			List<SelectDeviceGroupDTO> dgList = deviceGroupMapper.getDeviceGroups(id==null?companyId:id);
			List<SelectDeviceGroupDTO> gList = new ArrayList<>();
			if(id == null) {
				gList = deviceGroupService.getSelectDeviceGroupList(companyId);
			} else {
				List<SelectDeviceGroupDTO> dto = deviceGroupMapper.getDeviceGroups(id);
				gList.addAll(dto);
			}
			ResponseMessage.CODE_200.messageToProtocol(respProtocol);
			respProtocol.setDate(new XdaoDate());
			respProtocol.setData(gList);
			return respProtocol;
			
		} if(type == 3) { 
			// 安全管理员
			List<SelectUserDTO> smList = new ArrayList<SelectUserDTO>();
			// 网关
			List<SelectGatewayDTO> gList = new ArrayList<SelectGatewayDTO>();
			
			if(id == null) {
				ResponseMessage.CODE_400.messageToProtocol(respProtocol);
				return respProtocol;
			} else { // id不为空，则认为是根据设备组id获取
				smList =  userMapper.getSecurityManagers(id);
				gList =  gatewayMapper.getByGroupId(id);
			}
			
			Map<String, Object> returnMap = new HashMap<String, Object>();
			returnMap.put("securityManager", smList);
			returnMap.put("gateway", gList);
			ResponseMessage.CODE_200.messageToProtocol(respProtocol);
			respProtocol.setDate(new XdaoDate());
			respProtocol.setData(returnMap);
			return respProtocol;
			
		} else if(type == 4) { // 设备组的所属组（下拉选）
			List<SelectGroupDTO> gList = deviceGroupMapper.getGroupsByCompanyId(id==null?companyId:id);
			ResponseMessage.CODE_200.messageToProtocol(respProtocol);
			respProtocol.setDate(new XdaoDate());
			respProtocol.setData(gList);
			return respProtocol;
			
		} else if(type == 5) { // 下拉选报警描述类型
			List<SelectWarningDescDTO> wdList = warningProtocolMapper.getWarningDescs();
			ResponseMessage.CODE_200.messageToProtocol(respProtocol);
			respProtocol.setDate(new XdaoDate());
			respProtocol.setData(wdList);
			return respProtocol;
			
		} else if(type == 6) { // 角色： 管理员 /安全管理员/巡检员
			List<SelectRoleDTO> roleList = roleMapper.getSelectRoles();
			ResponseMessage.CODE_200.messageToProtocol(respProtocol);
			respProtocol.setDate(new XdaoDate());
			respProtocol.setData(roleList);
			return respProtocol;
			
		} else if(type == 7) { // 根据安全管理员id获取  安全巡检员
			List<SelectUserDTO> spList =  userMapper.getSecurityPatrol(id);
			ResponseMessage.CODE_200.messageToProtocol(respProtocol);
			respProtocol.setDate(new XdaoDate());
			respProtocol.setData(spList);
			return respProtocol;
			
		}
		else {
			ResponseMessage.CODE_400.messageToProtocol(respProtocol);
			return respProtocol;
		}
		
		
	}
	
	
	
	
	
}
